package com.jinan.yzht.simple.simples;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.io.File;
import java.sql.Types;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 东街浊酒づ
 * @date 2020/9/27
 */
@Component
public class FileUtils {
    @Value("${yzht.file.dir}")
    private String fileDir;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    private final static String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private final static String DATE_FORMAT4 = "yyyy";
    private final static String DATE_FORMAT6 = "yyyyMM";
    private final static String DATE_FORMAT8 = "yyyyMMdd";
    private final static String DATE_FORMAT_UUID = "yyyyMMddHHmmssSSS";

    public void fileDirParse() {
        File fileDir = new File("F:\\jinan_home\\files\\work\\produce_demo_dir");
        if (!fileDir.exists()) {
            System.out.println("当前文件路径无效！");
            return;
        }
        if (!fileDir.isDirectory()) {
            System.out.println("不是一个目录！");
            return;
        }
        File[] files;
        files = fileDir.listFiles();
        if (files.length == 0) {
            System.out.println("当前目录为空！");
            return;
        }
        List<Object[]> list = new ArrayList<Object[]>();
        for (int length = files.length, i = 0; i < length; i++) {
            File file = files[i];
            String fileName = file.getName();
            System.out.println("File Name:" + fileName);
            // 解析入库

            String[] fileParts = fileName.split("\\.");
            String suffix = fileParts[1];
            String[] filePart1Items = fileParts[0].split("_");
            String product = "VEGE";
            String subProduct = filePart1Items[0];
            String indexPeriod = filePart1Items[1].substring(0, 1);
            String region = filePart1Items[2];
            String satellite = filePart1Items[3];
            String sensor = filePart1Items[4];
            String resolution = filePart1Items[5];
            // TODO
            String productTime = filePart1Items[6];
            String format_flag = "";
            if (productTime.length() == 4) {
                format_flag = DATE_FORMAT4;
            } else if (productTime.length() == 6) {
                format_flag = DATE_FORMAT6;
            } else if (productTime.length() == 8) {
                format_flag = DATE_FORMAT4;
            }
            String createBy = System.getProperty("user.name");
            Date date = null;
            Date now = null;
            DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
            try {
                date = new SimpleDateFormat(format_flag).parse(productTime);
                now = dateFormat.parse(dateFormat.format(new Date()));
            } catch (ParseException e) {
                e.printStackTrace();
            }

            String uuid = String.valueOf(System.currentTimeMillis()) + i;
            java.sql.Timestamp timestamp = new java.sql.Timestamp(now.getTime());
            Object[] item =
                new Object[] {uuid, fileName, suffix, fileDir + fileName, product, subProduct, indexPeriod, region,
                    satellite, sensor, resolution, date, createBy, timestamp, createBy, timestamp, "备注"};
            list.add(item);
        }
        String sql =
            " INSERT INTO xdlt_product_ecology_monitor(id,file_name,suffix,file_path,product,subproduct,product_period,region,satellite,sensor,resolution,product_time,create_by,create_date,update_by,update_date,remarks) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        int[] args =
            new int[] {Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
                Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.DATE, Types.VARCHAR,
                Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR};
        jdbcTemplate.batchUpdate(sql, list, args);
    }
}
